const httpResponse = require('../modal/response');
const WebSocketServer = require('websocket').server;

/**
 * websocket message
 */
class MessageController {
    constructor(ctx) {
        this.ctx = ctx;
    }

    upgrade() {
        const {response, request} = this.ctx;

        const clientSecret = request.headers['Sec-WebSocket-Key'];

        if(!clientSecret) {
            return httpResponse.badRequest();
        }

        response.headers['upgrade'] = ' websocket';
        response.headers['Connection'] = 'Upgrade';
        response.headers['Sec-WebSocket-Accept'] = ''; 
    }

    pipe() {
        const wsServer = new WebSocketServer({
            httpServer: server,
            autoAcceptConnections: false
        });
    }
}

module.exports = MessageController;